#include "Common.h"

initSing(DatabaseLocalisation);

void DatabaseLocalisation::LoadLocalizedTable1(stdext::hash_map<ui32, TextLocalisationP1*>& fillmap, const char* table)
{
	QResult* res = CharacterDatabase.SafeQuery("Select * from %s", table);
	if(!res || !res->NumRows())
	{
		Log->Notice("Localisation", "Loaded 0 entries from %s", table);
		return;
	}

	ui32 oldentry = res->Fetch()[0];
	TextLocalisationP1* loc = new TextLocalisationP1;
	ui32 count = 0;
	do
	{
		ui32 tentry = res->Fetch()[0];
		if(tentry != oldentry)
		{
			fillmap[oldentry] = loc;
			loc = new TextLocalisationP1;
			oldentry = tentry;
		}

		TextLocalisationP1::TextLocale* tl = new TextLocalisationP1::TextLocale;
		tl->text[0] = res->Fetch()[1].GetChar();
		tl->localid = res->Fetch()[2].GetChar();
		loc->localisations.push_back(tl);
		++count;
	}
	while(res->NextRow());
	fillmap[oldentry] = loc;
	Log->Notice("Localisation", "Loaded %u entries from %s", count, table);
	delete res;
}

void DatabaseLocalisation::LoadLocalizedTable2(stdext::hash_map<ui32, TextLocalisationP2*>& fillmap, const char* table)
{
	QResult* res = CharacterDatabase.SafeQuery("Select * from %s", table);
	if(!res || !res->NumRows())
	{
		Log->Notice("Localisation", "Loaded 0 entries from %s", table);
		return;
	}

	ui32 oldentry = res->Fetch()[0];
	TextLocalisationP2* loc = new TextLocalisationP2;
	ui32 count = 0;
	do
	{
		ui32 tentry = res->Fetch()[0];
		if(tentry != oldentry)
		{
			fillmap[oldentry] = loc;
			loc = new TextLocalisationP2;
			oldentry = tentry;
		}

		TextLocalisationP2::TextLocale* tl = new TextLocalisationP2::TextLocale;
		tl->text[0] = res->Fetch()[1].GetChar();
		tl->text[1] = res->Fetch()[2].GetChar();
		tl->localid = res->Fetch()[3].GetChar();
		loc->localisations.push_back(tl);
		++count;
	}
	while(res->NextRow());
	fillmap[oldentry] = loc;
	Log->Notice("Localisation", "Loaded %u entries from %s", count, table);
	delete res;
}

void DatabaseLocalisation::LoadGossipLocals()
{
	LoadLocalizedTable1(GossipLocalMap, "gossiptexts_localised");
}

void DatabaseLocalisation::LoadItemLocals()
{
	LoadLocalizedTable2(ItemLocalMap, "items_localized");
}

void DatabaseLocalisation::LoadNpcLocals()
{
	LoadLocalizedTable2(NpcLocalMap, "creature_localized");
}

string* DatabaseLocalisation::GetStringLocale1(stdext::hash_map<ui32, TextLocalisationP1*>& searchmap, ui32 id, string locale)
{
	TextLocalisationP1* glc = searchmap[id];
	if(!glc)
		return 0;
	vector<TextLocalisationP1::TextLocale*>::iterator itr = glc->localisations.begin();
	vector<TextLocalisationP1::TextLocale*>::iterator end = glc->localisations.end();

	for( ; itr != end; ++itr)
	{
		if((*itr)->localid == locale)
			return &((*itr)->text[0]);
	}

	return 0;
}

string* DatabaseLocalisation::GetStringLocale2(stdext::hash_map<ui32, TextLocalisationP2*>& searchmap, ui32 id, string locale, ui32 pos)
{
	TextLocalisationP2* glc = searchmap[id];
	if(!glc)
		return 0;
	vector<TextLocalisationP2::TextLocale*>::iterator itr = glc->localisations.begin();
	vector<TextLocalisationP2::TextLocale*>::iterator end = glc->localisations.end();

	for( ; itr != end; ++itr)
	{
		if((*itr)->localid == locale)
			return &((*itr)->text[pos]);
	}

	return 0;
}

string* DatabaseLocalisation::GetGossipLocale(ui32 textid, string& locale)
{
	return GetStringLocale1(GossipLocalMap, textid, locale);
}

string* DatabaseLocalisation::GetItemNameLocale(ui32 textid, string& locale)
{
	return GetStringLocale2(ItemLocalMap, textid, locale, 0);
}

string* DatabaseLocalisation::GetItemDescriptionLocale(ui32 textid, string& locale)
{
	return GetStringLocale2(ItemLocalMap, textid, locale, 1);
}

string* DatabaseLocalisation::GetNpcNameLocale(ui32 npcid, string& locale)
{
	return GetStringLocale2(NpcLocalMap, npcid, locale, 0);
}

string* DatabaseLocalisation::GetNpcSubnameLocale(ui32 npcid, string& locale)
{
	return GetStringLocale2(NpcLocalMap, npcid, locale, 1);
}